<template>
    <div class="wrap">
        <!-- table数据 -->
        <div class="wrap-table">
            <el-table :data="tableData" border v-loading="tableLoading" :header-cell-style="{ background: '#F5F6FA' }">
                <!-- 索引 -->
                <el-table-column type="index" label="序号" width="60" align="center"> </el-table-column>
                <el-table-column prop="itemCode" label="编码" min-width="80"> </el-table-column>
                <el-table-column prop="itemName" label="名称" min-width="100"> </el-table-column>
                <el-table-column label="状态" min-width="80">
                    <template slot-scope="scope">
                        <el-tag :type="$options.filters.filterStatusClass(scope.row.status)">{{ scope.row.status | filterStatus }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" min-width="100"></el-table-column>
                <!-- 列操作 -->
                <el-table-column label="操作" width="100">
                    <template slot-scope="scope">
                        <el-button type="text" v-hasPerm="'system:dictionaryChild:edit'" @click="pageOperate('edit', scope.row)">更新</el-button>
                        <el-button type="text" v-hasPerm="'system:dictionaryChild:delete'" @click="pageOperate('delete', scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination :total="page.totalNum" :page.sync="page.pageNum" :limit.sync="page.pageSize" layout="total, sizes, prev, pager, next, jumper" @pagination="getTableData" />
        </div>
        <!-- dialog -->
        <el-dialog v-if="operateDialog" :title="operateTitle" :visible.sync="operateDialog" @close="closeOperateDialog" :width="`${operateWidth}px`" :close-on-press-escape="false" :close-on-click-modal="false">
            <el-form ref="operateForm" :model="operateForm" :rules="operateRules" label-width="100px">
                <el-form-item label="字典编码">
                    <el-input v-model="operateForm.dictCode" disabled />
                </el-form-item>
                <el-form-item label="编码" prop="itemCode">
                    <el-input v-model="operateForm.itemCode" placeholder="请输入编码" />
                </el-form-item>
                <el-form-item label="名称" prop="itemName">
                    <el-input v-model="operateForm.itemName" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="operateForm.status">
                        <el-radio v-for="(item, index) in statusOptions" :label="item.value" :key="index">{{ item.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="operateForm.remark" clearable type="textarea" :rows="1" placeholder="请输入备注"></el-input>
                </el-form-item>
                <el-form-item label="默认" prop="isDefault">
                    <el-radio-group v-model="operateForm.isDefault">
                        <el-radio :label="0">否</el-radio>
                        <el-radio :label="1">是</el-radio>
                    </el-radio-group>
                </el-form-item>
                <!-- <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="operateForm.sort" size="small" :min="0" />
                </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeOperateDialog">取 消</el-button>
                <el-button type="primary" :loading="operateLoading" @click="operateSubmit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { statusOptions } from '@/filters/options'
import { getDictionaryChildrenList, addOrUpdateDictionaryChildren, deleteDictionaryChildren } from '@/api/system'
export default {
    name: 'DictionaryManageData',
    props: {
        dictCode: {
            type: [String, Number],
            default: ''
        }
    },
    data() {
        return {
            page: {
                pageNum: 1,
                pageSize: 10,
                totalNum: 0
            },
            tableData: [],
            tableLoading: false,
            // 类型
            operateType: '',
            // 标题
            operateTitle: '',
            // 弹框
            operateDialog: false,
            // 宽度
            operateWidth: 550,
            // looading
            operateLoading: false,
            // 操作form
            operateForm: {
                id: '',
                dictCode: '',
                status: 0,
                remark: '',
                // -------
                // 编码
                itemCode: '',
                // 名称
                itemName: '',
                // 排序
                // sort: 1,
                // 默认（0否 1是）
                isDefault: 1
            },
            // 校验规则
            operateRules: {
                itemCode: [
                    { required: true, message: '请输入编码', trigger: ['blur'] }
                ],
                itemName: [
                    { required: true, message: '请输入名称', trigger: ['blur'] }
                ],
                status: [
                    { required: true, message: '请选择状态', trigger: ['change'] }
                ],
                // sort: [
                //     { required: true, message: '请输入排序', trigger: ['change'] }
                // ],
                isDefault: [
                    { required: true, message: '请选择是否默认', trigger: ['change'] }
                ],
            },
            // 状态选项
            statusOptions
        }
    },
    methods: {
        // 数据
        getTableData() {
            this.tableLoading = true
            getDictionaryChildrenList({
                pageNum: this.page.pageNum,
                pageSize: this.page.pageSize,
                dictCode: this.dictCode
            }).then(res => {
                this.tableLoading = false
                this.tableData = res.data.records
                this.page.totalNum = res.data.total
            }).catch(error => {
                this.tableLoading = false
            })
        },
        // 操作
        pageOperate(type, row) {
            this.operateType = type
            switch (type) {
                case 'search':
                    this.page.pageNum = 1
                    this.getTableData()
                    break;
                case 'refresh':
                    this.page = this.$options.data().page
                    this.getTableData()
                    break;
                case 'add':
                    this.operateDialog = true
                    this.operateTitle = '添加数据'
                    this.operateForm.dictCode = this.dictCode
                    break;
                case 'edit':
                    this.operateDialog = true
                    this.operateTitle = '修改数据'
                    this.operateForm.dictCode = row.dictCode
                    this.operateForm.id = row.id
                    this.operateForm.itemName = row.itemName
                    this.operateForm.itemCode = row.itemCode
                    this.operateForm.status = row.status
                    this.operateForm.remark = row.remark
                    // this.operateForm.sort = row.sort
                    this.operateForm.isDefault = row.isDefault
                    break;
                case 'delete':
                    this.$confirm('确认删除该数据?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        deleteDictionaryChildren({ id: row.id }).then(res => {
                            this.getTableData()
                            this.$message_tip(`删除成功`, 'success')
                        }).catch(error => { })
                    }).catch(() => { });
                    break;
                default:
                    break;
            }
        },
        // 提交
        operateSubmit() {
            this.$refs.operateForm.validate(valid => {
                if (valid) {
                    let formData = JSON.parse(JSON.stringify(this.operateForm))
                    if (this.operateType === 'add' || this.operateType === 'edit') {
                        this.operateLoading = true
                        addOrUpdateDictionaryChildren({
                            id: formData.id,
                            dictCode: formData.dictCode,
                            itemCode: formData.itemCode,
                            itemName: formData.itemName,
                            // sort: formData.sort,
                            isDefault: formData.isDefault,
                            status: formData.status,
                            remark: formData.remark,
                        }).then(res => {
                            this.getTableData()
                            this.$message_tip(`${this.operateTitle}成功`, 'success')
                            this.closeOperateDialog()
                        }).catch(error => {
                            this.closeOperateDialog()
                        })
                    }
                }
            })
        },
        // 关闭操作按钮
        closeOperateDialog() {
            this.operateLoading = false
            this.operateDialog = false
            this.operateTitle = ''
            this.operateType = ''
            this.operateForm = this.$options.data().operateForm
            this.$refs['operateForm'].resetFields()
        },
    },
    watch: {
        'dictCode': function (newVal, oldVal) {
             this.page = this.$options.data().page
            if(newVal) {
                this.getTableData()
            } else {
                this.tableData = []
            }
        }
    }
}
</script>

<style lang="scss" scoped>
</style>